import types from '../action-types'
import {getToken} from "../../request/auth";

const isLogin = Boolean(window.localStorage.getItem("authToken")) || Boolean(window.sessionStorage.getItem("authToken"));
const userInfo = JSON.parse(window.localStorage.getItem("userInfo")) || JSON.parse(window.sessionStorage.getItem("userInfo"));


const initState = {
    ...userInfo,
    authToken: isLogin,
    token: getToken(),
    isLoading: false,
};

export default (state = initState, action) => {
    switch (action.type) {

        case types.START_LOGIN:
            return {
                ...state,
                authToken: '',
                token: '',
                isLoading: true,
            };
        case types.LOGIN_SUCCESS:
            return {
                ...state,
                ...action.payload.userInfo,
                isLoading: false,
                authToken: action.token,
                token: action.token,

            };
        case types.LOGIN_FAILED:
            return {
                ...state,
                isLoading: false,
            };

        default:
            return state;
    }


};
